#ifndef __LOG_HPP__
#define __LOG_HPP__

#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <stdarg.h>

#define DEBUG   0
#define NORMAL  1
#define WARNING 2
#define ERROR   3
#define FATAL   4

#define FILE_NAME "./RingQueue.log"

const char* g_str[]={
    "DEBUG",
    "NORMAL",
    "WARNING",
    "ERROR",
    "FATAL"
};

void logMessage(int level,const char* format,...)
{
#ifndef DEBUG1
    if(level==0) return;
#endif
    //标准
    char stdBuff[1024];
    memset(stdBuff,0,sizeof(stdBuff));
    time_t tm=time(nullptr);
    snprintf(stdBuff,sizeof(stdBuff),"[%s][%d]",g_str[level],tm);

    //自定义
    char logBuff[1024];
    memset(logBuff,0,sizeof(logBuff));
    va_list args;
    va_start(args,format);
    vsnprintf(logBuff,sizeof(logBuff),format,args);
    va_end(args);

    //条件编译打印进不同文件
#ifdef PRINT
    printf("%s%s\n",stdBuff,logBuff);
#else
    FILE* fp=fopen(FILE_NAME,"a");
    if(fp==nullptr)
    {
        perror("open file");
    }
    fprintf(fp,"%s%s\n",stdBuff,logBuff);
#endif

}

#endif